

/******************** 定义类 ***************************************************************/
/*
    字典类
 */
function DataDictionary() {
    this.id = null;
    this.parent_id = 0;
    this.dictype = "";
    this.name = "";
    this.url = "";
    this.logo = [];
    this.remark = "";
    this.is_visible = 1;
    this.formated_create_time = ""; // 格式化的创建时间
    this.children = [];
    this.logo_ids = "";
}

// 将对象序列化为一个json对象，可以用于http请求或对象复制
DataDictionary.prototype.serialize = function() {
    var data = {};
    for(var key in this) {
        data[key] = this[key];
    }
    data.logo_ids = this.logo.map(function(elem){return elem.id}).join(",")
    return data;
}

// 用json数据为对象属性赋值
DataDictionary.prototype.parse = function(data) {
    Object.assign(this, JSON.parse(JSON.stringify(data)), {
        formated_create_time: new Date(parseInt(data.create_time)).toLocaleString(),
    });
}

DataDictionary.parseTree = function(tree) {
    // 改自己
    tree.__proto__ = DataDictionary.prototype;
    // 改儿子
    for(var i = 0, len = tree.children.length; i < len; i++) {
        DataDictionary.parseTree(tree.children[i]);
    }
    return tree;
}

// 暂时将属性存起来，做一次备份
DataDictionary.prototype.storageValue = function() {
    this.storage = this.serialize();
}

// 用备份的数据覆盖现有数据
DataDictionary.prototype.applyStorageValue = function() {
    if(this.storage) {
        this.parse(this.storage);
    }
}

DataDictionary.prototype.copy = function(target) {
    this.parse(target);
}

DataDictionary.copy = function(target) {
    return DataDictionary.parse(target);
}


// ******************** 类静态属性与方法 **********************************************
// 解析json数据，返回一个对象
DataDictionary.parse = function(data) {
    var o = new DataDictionary();
    o.parse(data);
    return o;
}

// 解析json数组，返回对象数组
DataDictionary.parseList = function(list) {
    var len = list.length;
    var arr = new Array(len);
    for(var i = 0; i < len; i++) {
        arr[i] = DataDictionary.parse(list[i]);
    }
    return arr;
}

// ******************** 数据库存储方法 **********************************************
// Admin.prototype.save = function(callback) {
//     var that = this;
//     var action = this.mode == "add" ? "article_add" : "article_modify";
//     api.post(action, this.serialize(), function(res) {
//         that.id = res.id;
//         if(callback) callback();
//     });
// }



// ******************** 导出 **********************************************
export default DataDictionary